﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class TeamSponsorshipDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.TeamSponsorshipDetails_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx"); 
            
            if (Request.QueryString["ConfirmNote"] != null)
            {
                this.Message.Text = Request.QueryString["ConfirmNote"].ToString();                
            }
            
            
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            string SponsorId = null;
            
            SponsorId = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetTeamSponsorId", ou.UserId));
            if ((SponsorId == null))
                Response.Redirect("~/Default.aspx");

            if (!IsPostBack)
            {
                this.GetTeamSponsorDetails.SelectParameters.Add("UserId", ou.UserId);
                this.SponsorshipProposalsSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.SuggestionsSumAndDateSataSource.SelectParameters.Add("UserId", ou.UserId);
            
                // deal with niewbie tips
                if ((int)Session["NewbieTipStage"] == 3)
                {
                    Session["NewbieTipStage"] = 4;
                    Session["NewbieTipText"] = GND.ReadNivdalStringData("TipId", "Language", "dbo.Nivdal_NewbieTipsGetTip", "4", ou.Language);
                    GND.ExecNivdalSQL("UserId", "StageId", "Nivdal_NewbieTipsSetStage", ou.UserId, "4");
                }
            }

            //Response.Write("SponsorId is :" + SponsorId);
            SuggestionsPanel.Visible = (SponsorshipProposalsGridView.Rows.Count > 0);
        }



        protected bool StatusString(Object status)
        {
            if (status.ToString() == "1")
                return true;
            else
                return false;


        }


        protected void ApproveSponsor(string ProposalId)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = GND.ReadNivdalStringData("@UserId", "@ProposalId", "@Language", "Nivdal_AcceptSponsorContract", ou.UserId, ProposalId, ou.Language);

            Response.Redirect("TeamSponsorshipDetails.aspx?ConfirmNote=" + Answer);

        }



        protected void Accept(Object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "AcceptSponsor")
            {
                ApproveSponsor(e.CommandArgument.ToString());
            }
            else
                Response.Redirect("TeamSponsorshipDetails.aspx?ConfirmNote=error");
        }


        protected void SponsorshipProposalsGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;

        }


        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string AcceptLinkButtonOnClientClick(string SponsorName)
        {
            return ("return confirm('" + Resources.Resource.TeamSponsorshipDetails_AcceptLinkButton + " " + SponsorName + "?')");
        }

        protected string SponsorshipSuggestionsLabelTextPart1()
        {
            return (Resources.Resource.TeamSponsorshipDetails_SponsorshipSuggestionsLabelTextPart1);
        }

        protected string SponsorshipSuggestionsLabelTextPart2()
        {
            return (Resources.Resource.TeamSponsorshipDetails_SponsorshipSuggestionsLabelTextPart2);
        }
        protected string SponsorshipSuggestionsLabelTextPart3()
        {
            return (Resources.Resource.TeamSponsorshipDetails_SponsorshipSuggestionsLabelTextPart3);
        }


        protected string SponsorshipSuggestionsLabelText(string ContractSum, string EndTime, string ContractStatus)
        {
            string Text = Resources.Resource.TeamSponsorshipDetails_SponsorshipSuggestionsLabelTextPart1 +
                " " + (int.Parse(ContractSum)).ToString("N0") + " " + Resources.Resource.Atuks + ". " +
                        Resources.Resource.TeamSponsorshipDetails_SponsorshipSuggestionsLabelTextPart2;
            if (ContractStatus == "1")
            {
                Text += "<br />" + Resources.Resource.TeamSponsorshipDetails_SponsorshipSuggestionsLabelTextPart3 + ":";
            }

            return (Text);
        }
    }
}